<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

	public $uses = array();

    function beforeFilter() {
        parent::beforeFilter();
				$this->Auth->allow('add');
    }

    public function add()
    {


        $this->layout = 'default';

        if ($this->request->is('post')) {
					if($this->request->data['User']['password'] == $this->request->data['User']['confirm_password'])
					{
						unset($this->request->data['User']['confirm_password']);
						$this->request->data['User']['role'] = 'candidato';
						$this->User->create();
						//die(debug($this->request->data));
						if ($this->User->save($this->request->data)) {
							$this->Auth->login();
							$this->Session->setFlash(__('Criado com sucesso! Preencha seu currículo abaixo.'));
							$this->redirect(array('controller'=>'curriculos', 'action' => 'add'));
						} else {
							$this->Session->setFlash(__('Ocorreu um erro no seu cadastro'));
						}
					}
					else {
						$this->Session->setFlash(__('Senhas não conferem.'));
					}
				}

    }

		public function admin_add()
		{
			$this->layout = 'admin';

			if ($this->request->is('post')) {
				$this->User->create();
				if ($this->User->save($this->request->data)) {
					$this->Session->setFlash(__('The user has been saved'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
				}
			}
		}

    public function login()
    {
        $this->layout = 'login';
        if ($this->Auth->login())
        {
            $this->redirect($this->Auth->redirect());
        }
        else
        {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }

}
